package com.book.servlet.pages;

import com.book.entity.Admin;
import com.book.service.BookService;
import com.book.service.impl.BookServiceImpl;
import com.book.utils.ThymeleafUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.thymeleaf.context.Context;

import java.io.IOException;

@WebServlet(name = "BookServlet", value = "/books")
public class BookServlet extends HttpServlet {
    BookService bookService;

    @Override
    public void init() throws ServletException {
        bookService =new BookServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Context context = new Context();
        Admin admin = (Admin) request.getSession(true).getAttribute("user");
        context.setVariable("nickname", admin.getNickname());
        //Map<Book, Boolean> map = bookService.getBookList();
        //context.setVariable("bookList",map.keySet());
        //context.setVariable("bookListStatus",new ArrayList<>(map.values()));
        context.setVariable("bookList",bookService.getBookList());
        ThymeleafUtil.process("books.html", context, response.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
